package com.xmg.mgrsite.base;

import com.alibaba.fastjson.JSON;
import com.xmg.p2p.base.domain.SystemDictionary;
import com.xmg.p2p.base.domain.SystemDictionaryItem;
import com.xmg.p2p.base.query.SystemDictionaryQueryObject;
import com.xmg.p2p.base.service.SystemDictionaryService;
import com.xmg.p2p.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @BelongsProject: jinrong-website
 * @BelongsPackage: com.xmg.mgrsite.base
 * @author: Andy
 * @date: 2021.3.18 10:51
 * @Description: 数据字典控制器
 * @since JDK 1.8
 */
@Controller
public class SystemDictionaryController {
    @Autowired
    private SystemDictionaryService systemDictionaryService;

    /**
     * 数据字典分类列表
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/systemDictionary_list")
    public String systemDictionary_list(@ModelAttribute("qo") SystemDictionaryQueryObject qo,
                                   Model model){
        // 调用字典业务层查询queryDics方法
        model.addAttribute("pageResult",this.systemDictionaryService.queryDics(qo));
        return "systemdic/systemDictionary_list";
    }

    /**
     * 数据字典分类修改
     * @param sd
     * @return
     */
    @ResponseBody
    @RequestMapping("/systemDictionary_update")
    public JSONResult systemDictionary_update(@ModelAttribute("sd") SystemDictionary sd){
        JSONResult jsonResult = new JSONResult();
        // 调用字典业务层查询saveOrUpdateDic方法
        try {
            this.systemDictionaryService.saveOrUpdateDic(sd);
        }catch (Exception ex){
            jsonResult.setMsg("修改失败");
            jsonResult.setSuccess(false);
            ex.printStackTrace();
        }
        return jsonResult;
    }

    /**
     * 数据字典明细列表
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("systemDictionaryItem_list")
    public String systemDictionaryItem_list(@ModelAttribute("qo") SystemDictionaryQueryObject qo,
                                            Model model){
        // 第一次访问的时候，获取的是全部明细（因为分类和关键字都为空）
        // 如果点击分类就按照分类编号进行查询，如果在查询框就是按照查询进行查询。
        model.addAttribute("pageResult",this.systemDictionaryService.queryDicsItems(qo));
        // 获取数据字典分类的内容
        model.addAttribute("systemDictionaryGroups",this.systemDictionaryService.listDics());
        return "systemdic/systemDictionaryItem_list";
    }

    @ResponseBody
    @RequestMapping("systemDictionaryItem_update")
    public JSONResult systemDictionaryItem_update(SystemDictionaryItem sd){
        JSONResult jsonResult = new JSONResult();
        try {
            this.systemDictionaryService.saveOrUpdateDicItem(sd);
            jsonResult.setSuccess(true);
        }catch (Exception ex){
            jsonResult.setMsg("修改或添加失败");
            jsonResult.setSuccess(false);
            ex.printStackTrace();
        }
        return jsonResult;
    }
}
